
// var custId=document.querySelector(".cusId").innerText.trim().replace(/\s/g,"");
var customer=document.querySelector(".customer").innerText.trim().replace(/\s/g,"");

console.log("custId:====================" + custId);
// var cusId = custId;
var currentPageNum;
var gen="http://localhost:8080/zyx";
function getByPage(pageNum){
    // console.log("cusId" + cusId);
    if(custId==""||custId==undefined){
        custId=0;
    }
    console.log("++++++++++++++++++" + custId);
    $(".notes-lists-hot").html("");
    var url;
    if(typeof (pageNum)!="number" || typeof (pageNum) == undefined){
        console.log("232131312312:" + typeof (pageNum));
        url="index/customer/" + custId;
        console.log("url:" + url);
        pageNum=1;
    }else {
        console.log("123456789:" + typeof (pageNum));
        url="index/customer/"+ custId +"/page/"+pageNum;
        console.log("url:" + url);
    }
    currentPageNum=pageNum;
    $.get(
        url,
        null,
        function (result){
            console.log(result);
            if(result.code === 200){
                for (let i = 0; i < result.data.list.length; i++) {
                    var praiseCount=result.data.list[i].travelNotes.travelNotesPraiseCount;
                    var notesPraise=result.data.list[i].notesPraise;
                    if (praiseCount==null){
                        praiseCount=0;
                    }
                    var contentHtml = null;
                    if(notesPraise == null){
                        contentHtml =
                            '<div class="item">' +
                            '<div class="item-img">' +
                            '<a href="http://localhost:8080/zyx/index/notes/details/'+result.data.list[i].travelNotes.travelNotesId+'/'+result.data.list[i].customer.custId+'" class="main-img">' +
                            '<img src="'+result.data.list[i].travelNotes.travelNotesMainImg+'">' +
                            '</a>' +
                            '</div>' +
                            '<div class="notes">' +
                            '<div class="notes-details">' +
                            '<a href="http://localhost:8080/zyx/index/notes/details/'+result.data.list[i].travelNotes.travelNotesId+'/'+result.data.list[i].customer.custId+'" class="notes-title">'+result.data.list[i].travelNotes.travelNotesTitle+'</a>' +
                            '<p class="notes-text">'+result.data.list[i].travelNotes.travelNotesOthrt1+'</p>' +
                            '<span class="user">' +
                            '<div class="a">' +
                            '<a href="#"><img src="'+result.data.list[i].customer.custProfile+'"></a>' +
                            '<a href="#" class="user-name" style="font-family: Arial;">'+result.data.list[i].customer.custNickname+'</a>' +
                            '<img src="images/home/loc1.svg">' +
                            '<a href="#">'+result.data.list[i].travelNotes.travelNotesOthrt2+'</a>' +
                            '</div>' +
                            '<em class="praise-count">'+praiseCount+'</em>' +
                            '<a id="'+i+'" href="javascript: void(0)" class="praise-btn" onclick="praise(this,'+result.data.list[i].travelNotes.travelNotesId+','+custId+')"><i></i></a>' +
                            // '<a id="'+i+'" href="javascript: void(0)" class=" praise-btn2" onclick="praise(this,'+result.data.list[i].travelNotes.travelNotesId+','+custId+')"><i></i></a>' +
                            '</span>' +
                            '</div>' +
                            '</div>' +
                            '</div>';
                    }else {
                        contentHtml =
                            '<div class="item">' +
                            '<div class="item-img">' +
                            '<a href="http://localhost:8080/zyx/index/notes/details/'+result.data.list[i].travelNotes.travelNotesId+'/'+result.data.list[i].customer.custId+'" class="main-img">' +
                            '<img src="'+result.data.list[i].travelNotes.travelNotesMainImg+'">' +
                            '</a>' +
                            '</div>' +
                            '<div class="notes">' +
                            '<div class="notes-details">' +
                            '<a href="http://localhost:8080/zyx/index/notes/details/'+result.data.list[i].travelNotes.travelNotesId+'/'+result.data.list[i].customer.custId+'" class="notes-title">'+result.data.list[i].travelNotes.travelNotesTitle+'</a>' +
                            '<p class="notes-text">'+result.data.list[i].travelNotes.travelNotesOthrt1+'</p>' +
                            '<span class="user">' +
                            '<div class="a">' +
                            '<a href="#"><img src="'+result.data.list[i].customer.custProfile+'"></a>' +
                            '<a href="#" class="user-name" style="font-family: Arial;">'+result.data.list[i].customer.custNickname+'</a>' +
                            '<img src="images/home/loc1.svg">' +
                            '<a href="#">'+result.data.list[i].travelNotes.travelNotesOthrt2+'</a>' +
                            '</div>' +
                            '<em class="praise-count">'+praiseCount+'</em>' +
                            // '<a id="'+i+'" href="javascript: void(0)" class="praise-btn" onclick="praise(this,'+result.data.list[i].travelNotes.travelNotesId+','+custId+')"><i></i></a>' +
                            '<a id="'+i+'" href="javascript: void(0)" class="praise-btn2" onclick="praise(this,'+result.data.list[i].travelNotes.travelNotesId+','+custId+')"><i></i></a>' +
                            '</span>' +
                            '</div>' +
                            '</div>' +
                            '</div>';
                    }

                    $(".notes-lists-hot").append(contentHtml);
                }
                var prePage = result.data.prePage;
                var nextPage = result.data.nextPage;
                var pageEle;
                layui.use('laypage', function(){
                    var laypage = layui.laypage;
                    //执行一个laypage实例
                    laypage.render({
                        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                        ,count: result.data.total //数据总数，从服务端得到
                        ,theme: '#38c4ff'
                        ,limit: result.data.pageSize//每页显示
                        ,curr: result.data.pageNum
                        ,jump:function (obj,first){
                            if (!first){
                                result.data.pageNum=obj.curr;
                                getByPage(result.data.pageNum)
                            }
                        }
                    });
                });
                /*点赞*/
                // var flag=0;
                // $(".praise-btn").click(function (){
                //         var count = $(".praise-count");
                //         if (flag == 0) {
                //             if (this.id == 0) {
                //                 var c = parseInt(count[0].innerText) + 1;
                //                 count[0].innerText = c;
                //                 result.data.list[0].travelNotesPraiseCount = c;
                //                 var data = result.data.list[0];
                //                 console.log(data.travelNotesId)
                //                 var id = data.travelNotesId;
                //                 var url = "http://localhost:8080/zyx/index/praise/count/" + id;
                //                 $.get(
                //                     url,
                //                     null,
                //                     function (result) {
                //                         console.log(1111111111);
                //                     },
                //                     "json"
                //                 )
                //             }
                //             flag++;
                //         } else {
                //             if (this.id == 0) {
                //                 var c = parseInt(count[0].innerText) - 1;
                //                 count[0].innerText = c;
                //             }
                //             flag--;
                //         }
                // })
            }
        },
        "json"
    )
}
function getByPageOederByDate(pageNum){
    $(".notes-lists-hot").html("");
    var url;
    if(typeof (pageNum)!="number"){
        url="index/date";
        pageNum=1;
    }else {
        url="index/date/page/"+pageNum;
    }
    $.get(
        url,
        null,
        function (result){
            if(result.code === 200){
                for (let i = 0; i < result.data.list.length; i++) {
                    var praiseCount=result.data.list[i].travelNotes.travelNotesPraiseCount;
                    if (praiseCount==null){
                        praiseCount=0;
                    }
                    var content = String(result.data.list[0].travelNotes.travelNotesText);
                    console.log(content)
                    var contentHtml =
                        '<div class="item">' +
                        '<div class="item-img">' +
                        '<a href="http://localhost:8080/zyx/index/notes/details/'+result.data.list[i].travelNotes.travelNotesId+'/'+result.data.list[i].customer.custId+'" class="main-img">' +
                        '<img src="'+result.data.list[i].travelNotes.travelNotesMainImg+'">' +
                        '</a>' +
                        '</div>' +
                        '<div class="notes">' +
                        '<div class="notes-details">' +
                        '<a href="http://localhost:8080/zyx/index/notes/details/'+result.data.list[i].travelNotes.travelNotesId+'/'+result.data.list[i].customer.custId+'" class="notes-title">'+result.data.list[i].travelNotes.travelNotesTitle+'</a>' +
                        '<p class="notes-text">'+result.data.list[i].travelNotes.travelNotesOthrt1+'</p>' +
                        '<span class="user">' +
                        '<div class="a">' +
                        '<a href="#"><img src="'+result.data.list[i].customer.custProfile+'"></a>' +
                        '<a href="#" class="user-name" style="font-family: Arial;">'+result.data.list[i].customer.custNickname+'</a>' +
                        '<img src="images/home/loc1.svg">' +
                        '<a href="#">'+result.data.list[i].travelNotes.travelNotesOthrt2+'</a>' +
                        '</div>' +
                        '<em class="praise-count">'+praiseCount+'</em>' +
                        '<a id="'+i+'" class="praise-btn"><i></i></a>' +
                        '</span>' +
                        '</div>' +
                        '</div>' +
                        '</div>'
                    $(".notes-lists-hot").append(contentHtml)
                }
                /*分页*/
                // var prePage = result.data.prePage;
                // var nextPage = result.data.nextPage;
                // var pageEle;
                // if(!result.data.hasPreviousPage){
                //     pageEle = '' +
                //         '<span>'+'共'+result.data.pages+'页/'+result.data.total+'条'+'</span>' +
                //         '<span>'+'当前是第'+ result.data.pageNum +'页'+'</span>' +
                //         '<a href="javascript:getByPageOederByDate('+nextPage+')">下一页</a>'
                //     $(".page").html(pageEle);
                // }else if(!result.data.hasNextPage){
                //     pageEle = '' +
                //         '<span>'+'共'+result.data.pages+'页/'+result.data.total+'条'+'</span>' +
                //         '<a href="javascript:getByPageOederByDate('+prePage+')">上一页</a>'+
                //         '<span>'+'当前是第'+ result.data.pageNum +'页'+'</span>'
                //     $(".page").html(pageEle);
                // }else {
                //     pageEle = '' +
                //         '<span>'+'共'+result.data.pages+'页/'+result.data.total+'条'+'</span>' +
                //         '<a href="javascript:getByPageOederByDate('+prePage+')">上一页</a>'+
                //         '<span>'+'当前是第'+ result.data.pageNum +'页'+'</span>'+
                //         '<a href="javascript:getByPageOederByDate('+nextPage+')">下一页</a>'
                //     $(".page").html(pageEle);
                // }

                layui.use('laypage', function(){
                    var laypage = layui.laypage;
                    //执行一个laypage实例
                    laypage.render({
                        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                        ,count: result.data.total //数据总数，从服务端得到
                        ,theme: '#38c4ff'
                        ,limit: result.data.pageSize//每页显示
                        ,curr: result.data.pageNum
                        ,jump:function (obj,first){
                            if (!first){
                                result.data.pageNum=obj.curr;
                                getByPageOederByDate(result.data.pageNum)
                            }
                        }
                    });
                });
                /*点赞*/
                $(".praise-btn").click(function (){
                    this.innerHTML="提交中";
                    this.onclick="";
                    this.removeAttribute("href");
                })
            }
        },
        "json"
    )
}

//manhout推荐
function getRecommend(custId){
    $(".recommendSights").html("");
    var prams={
        "custId":custId,
        "num":2
    }
    var url=gen+"/recommendByCust";
    $.get(
        url,
        {"custId":custId,"num":2},
        function (result){
            console.log(result)
            for (let i = 0; i < result.length; i++) {
                var liEle=
                    `<li>
                        <div class="ad-title">
                            <span>为您推荐</span>
                        </div>
                        <div class="ad-img">
                            <a href="`+gen+`/sights/details/`+result[i].travelSightsId+`"><img src="`+result[i].travelSightsMainImg+`"></a>
                        </div>
                        <div class="ad-details">
                            <a href="`+gen+`/sights/details/`+result[i].travelSightsId+`">`+result[i].travelSightsName+`</a>
                            <p>`+result[i].travelSightsDescribe+`</p>
                        </div>
                    </li>`
                $(".recommendSights").append(liEle);
            }
        },
        "json"
    )
}

//点赞
function  praise(a,travelNotesId,custId) {
    if( customer == "null"){
        alert("请先完成登录");
        console.log("praise a:" + a);
        console.log("praise travelNotesId:" + travelNotesId);
        console.log("praise custId:" + custId);
    }else {
        var status;
        // if($(a).hasClass('praise-btn')){
        //     status = 0;
        // }else {
        //     status = 1
        // }
        // var praiseCount4 = $(a).prev().text();
        // console.log("praiseCount4:" + praiseCount4);
        var praiseCount6 = parseInt($(a).prev(".praise-count").text());
        console.log("praiseCount6:" + praiseCount6);
        //status = 0 点赞;status = 1 取消点赞
        //未点赞  && status == 0
        if($(a).hasClass('praise-btn')){
            status = 0;
            var c = praiseCount6 + 1;
            // $(a).prev(".praise-count").innerText = c;
            $(a).prev(".praise-count").text(c);
            // $(a).prev(".praise-count").val(c);
            console.log("c:" + $(a).prev(".praise-count").text());
            // status += 1;
            console.log("status=====" + status);
            $(a).removeClass("praise-btn");
            $(a).addClass("praise-btn2");

        }else {
            var c = praiseCount6 - 1;
            // $(a).prev(".praise-count").innerText = c;
            $(a).prev(".praise-count").text(c);
            console.log("c:" + $(a).prev(".praise-count").text());
            $(a).addClass("praise-btn");
            $(a).removeClass("praise-btn2");
            status = 1;
        }

        var url = gen + "/cusId/"+ custId +"/travelNotesId/"+ travelNotesId +"/status/" + status;
        $.get(url,null,function (result) {
            console.log(JSON.stringify(result));
        },"json");
    }

}


















